package com.sgb.kjwl.view.widget;

import android.app.Activity;
import android.app.Dialog;
import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.graphics.BitmapFactory;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import com.mob.MobSDK;
import com.sgb.kjwl.R;
import com.swgk.core.util.MToast;
import java.util.HashMap;
import cn.sharesdk.dingding.friends.Dingding;
import cn.sharesdk.dingding.utils.DingdingClientNotExistException;
import cn.sharesdk.framework.Platform;
import cn.sharesdk.framework.PlatformActionListener;
import cn.sharesdk.framework.ShareSDK;
import cn.sharesdk.tencent.qq.QQ;
import cn.sharesdk.tencent.qq.QQClientNotExistException;
import cn.sharesdk.wechat.friends.Wechat;
import cn.sharesdk.wechat.moments.WechatMoments;
import cn.sharesdk.wechat.utils.WechatClientNotExistException;

/**
 * 询价分享-分享底部弹窗实现
 */
public class ShareCustomerInquiryDialog {
    private String url, context;
    private Activity activity;
    private Dialog dialog;
    private View rootView;

    public ShareCustomerInquiryDialog(Activity Context) {
        this.activity = Context;
        MobSDK.submitPolicyGrantResult(true, null);

        dialog = new Dialog(Context, R.style.RoundCornerDialog);
        //2、设置布局
        rootView = View.inflate(Context, R.layout.share_customer_inquiry_dialog, null);

        dialog.setContentView(rootView);
        Window window = dialog.getWindow();
        //设置弹出位置
        window.setGravity(Gravity.BOTTOM);
        //设置弹出动画
        window.setWindowAnimations(R.style.ActionSheetDialogAnimation);
        //设置对话框大小
        window.setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        dialog.show();

        initView();
    }

    public void init(String url,String context) {
        this.url = url+"&type=0";
        this.context = context;

    }

    private void initView() {
        rootView.findViewById(R.id.tv_wechat).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ShareSDKContent(ShareSDK.getPlatform(Wechat.NAME));
            }
        });
        rootView.findViewById(R.id.tv_wechat_moments).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ShareSDKContent(ShareSDK.getPlatform(WechatMoments.NAME));
            }
        });
        rootView.findViewById(R.id.tv_dingding).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ShareSDKContent(ShareSDK.getPlatform(Dingding.NAME));
            }
        });
        rootView.findViewById(R.id.tv_qq).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ShareSDKContent(ShareSDK.getPlatform(QQ.NAME));
            }
        });
        rootView.findViewById(R.id.tv_copy).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //获取剪贴板管理器：
                ClipboardManager cm = (ClipboardManager) activity.getSystemService(Context.CLIPBOARD_SERVICE);
                // 创建普通字符型ClipData
                ClipData mClipData = ClipData.newPlainText("Label", url);
                // 将ClipData内容放到系统剪贴板里。
                cm.setPrimaryClip(mClipData);
                MToast.showToast(activity, "复制成功");
                dialog.dismiss();
            }
        });

        rootView.findViewById(R.id.tv_cancel).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.cancel();
            }
        });
        rootView.findViewById(R.id.rel).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dialog.dismiss();
            }
        });
    }

    private void ShareSDKContent(Platform platform) {

        Platform.ShareParams sp = new Platform.ShareParams();
        sp.setUrl(url);
        sp.setText("如果你有货，就来给我报价吧~");
        sp.setTitle(context);
        sp.setTitleUrl(url);
        sp.setImageData(BitmapFactory.decodeResource(activity.getResources(), R.mipmap.ic_share));
        sp.setShareType(Platform.SHARE_WEBPAGE);
        platform.setPlatformActionListener(new PlatformActionListener() {
            @Override
            public void onComplete(Platform platform, int i, HashMap<String, Object> hashMap) {
                MToast.showToast(activity, "分享成功");
                dialog.dismiss();
            }

            @Override
            public void onError(Platform platform, int i, Throwable throwable) {
                dialog.dismiss();
                String msg = "软件未安装";
                if (throwable instanceof WechatClientNotExistException) {
                    msg = "微信未安装";
                }
                if (throwable instanceof DingdingClientNotExistException) {
                    msg = "钉钉未安装";
                }
                if (throwable instanceof QQClientNotExistException || "QQClientNotExistException".equals(throwable.getMessage())) {
                    msg = "QQ未安装";
                }
                String finalMsg = msg;
                activity.runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        MToast.showToast(activity, finalMsg);
                    }
                });
            }

            @Override
            public void onCancel(Platform platform, int i) {
                MToast.showToast(activity, "分享取消");
                dialog.dismiss();
            }
        });
        platform.share(sp);
    }

}